package com.jiaxue.admin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.map.util.JSONPObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.jiaxue.admin.model.ReturnObj;
import com.jiaxue.admin.service.CoinService;
import com.jiaxue.flow.model.CommonProduct;
import com.jiaxue.flow.utils.GetFlowReCode;
import com.jiaxue.flow.utils.JsonUtil;

@Controller
public class CoinStatisticsController {
	
	private static final Integer YIDONG=2;
	private static final Integer LIANTONG=3;
	private static final Integer DIANXIN=4;
	
	@Autowired
	private CoinService coinService;
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/getStatus")
	public @ResponseBody JSONPObject getStatus(@RequestParam String callback) {
		ReturnObj roObj=null;
		try{
			Map rmapMap=null;
			List<Map<String,Integer>> typeSum = coinService.getIphoneTypeSum();
			rmapMap=getTotalDetail(typeSum);
			Integer coinSum = coinService.getCoinSum();
			rmapMap.put("coinsum", coinSum);
			Integer coinConsume = coinService.getCoinConsume();
			rmapMap.put("coinconsume", coinConsume);
			rmapMap.put("coinremain", coinSum-coinConsume);
			roObj=new ReturnObj(200, "获取成功", rmapMap);
		}catch(Exception e){
			e.printStackTrace();
			roObj=new ReturnObj(-1, e.getMessage(), null);
		}
		return new JSONPObject(callback, roObj);
	}
	/**
	 * 获取三大运行商汇总数据
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/statisticalSummary")
	public @ResponseBody JSONPObject statisticalSummary(@RequestParam  Integer pageNum,
			@RequestParam Integer pageSize,@RequestParam String callback,
			@RequestParam(required=false)  String starttime,@RequestParam(required=false)  String endtime) {
		//List<PaymentOrder> list = coinService.iphoneSumList(pageNum,pageSize);
		ReturnObj roObj=null;
		try {
			PageInfo pageInfo= coinService.selectClientSumByPage(starttime, endtime, pageNum, pageSize);
			roObj=new ReturnObj(200, "获取成功", pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			roObj=new ReturnObj(-1, e.getMessage(), null);
		}
		
		return new JSONPObject(callback, roObj) ;
	} 
	/**
	 * 验证订单是否成功
	 * @param orderid
	 * @param iprotypeid
	 * @param czphone
	 * @param callback
	 * @return
	 */
	@RequestMapping(value="/verifyOrderStatus")
	public @ResponseBody JSONPObject verify(
			@RequestParam  String orderid,
			@RequestParam Integer iprotypeid,
			@RequestParam String czphone,
			@RequestParam String callback){
		ReturnObj roObj=null;
		try{
			
			CommonProduct cp=new CommonProduct();
			//2 移动 3 联通 4 电信
			cp.setIprotypeid(iprotypeid);//套餐类型
			cp.setOrderId(orderid);//订单号
			String ret = GetFlowReCode.checkCode(cp);//JSON串  pkg套餐类型 
			//{"orderId":"","phoneNo":"","pkgNo":"","code":"1001"}
			if("".equalsIgnoreCase(ret)||ret==null){
				roObj=new ReturnObj(200, "根据套餐类型和订单号在运营商处查询不到数据", false);
				return new JSONPObject(callback, roObj) ;
			}
			JSONObject jsonObject = JSON.parseObject(ret);
			String code = jsonObject.getString("code");
			String phoneNo=jsonObject.getString("phoneNo");
			String pkgNo = jsonObject.getString("pkgNo");
			
			if(phoneNo==null||!phoneNo.equalsIgnoreCase(czphone)){
				roObj=new ReturnObj(200, "充值手机号与运营商记录不匹配", false);
				return new JSONPObject(callback, roObj) ;
			}
			//通过订单号查找pkgNo
			String pkgNoByOrderId = coinService.getPkgNoByOrderId(orderid);
			if(pkgNo==null||!pkgNoByOrderId.equalsIgnoreCase(pkgNo)){
				roObj=new ReturnObj(200, "系统套餐类型与运行商套餐类型不匹配", false);
				return new JSONPObject(callback, roObj) ;
			}
			boolean result = JsonUtil.isSucess(code);
			String descByCode = coinService.getCodeDescByCode(code);
			roObj=new ReturnObj(200, descByCode, result);
			
		}catch(Exception e){
			e.printStackTrace();
			roObj=new ReturnObj(-1, e.getMessage(), null);
		}
		
		return new JSONPObject(callback, roObj) ;
	}
	/**
	 * 获取统计的详细信息
	 * @param pageNum
	 * @param pageSize
	 * @param callback
	 * @param starttime
	 * @param endtime
	 * @param phoneno
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/statisticalDetail")
	public @ResponseBody JSONPObject statisticalDetail(@RequestParam  Integer pageNum,
			@RequestParam Integer pageSize,@RequestParam String callback,
			@RequestParam(required=false)  String starttime,@RequestParam(required=false)  String endtime,
			@RequestParam(required=false) String phoneno){
		ReturnObj roObj=null;
		try {
			PageInfo pageInfo = coinService.selectStatisticsDetailByDateAndPhoneNo(starttime,endtime,phoneno,pageNum,pageSize);
			roObj=new ReturnObj(200, "获取成功", pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			roObj=new ReturnObj(-1, e.getMessage(), null);
		}
		return new JSONPObject(callback, roObj) ;
		
	}
	/**
	 * 获取用户日志记录
	 * @param pageNum
	 * @param pageSize
	 * @param callback
	 * @param id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/clientDetail")
	public @ResponseBody JSONPObject clientDetail(@RequestParam  Integer pageNum,
			@RequestParam Integer pageSize,@RequestParam String callback,
			@RequestParam Integer id){
		ReturnObj roObj=null;
		try {
			PageInfo pageInfo = coinService.selectClientDetailById(id,pageNum,pageSize);
			roObj=new ReturnObj(200, "获取成功", pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			roObj=new ReturnObj(-1, e.getMessage(), null);
		}
		return new JSONPObject(callback, roObj) ;
	}
	
	
	@SuppressWarnings("unchecked")
	private Map getTotalDetail(List<Map<String, Integer>> list){
		Integer totalInteger=0;
		Map rmap=new HashMap();
		for(Map map :list){
			rmap.put(map.get("type"), map.get("count"));
			totalInteger+=Integer.parseInt(map.get("count").toString());
		}
		rmap.put("total", totalInteger);
		return rmap;
	}
	
}
